package com.gkzf.ai.module.crm.controller.app.simpleCredit;

import com.gkzf.ai.framework.common.pojo.CommonResult;
import com.gkzf.ai.framework.common.pojo.PageResult;
import com.gkzf.ai.framework.common.util.object.BeanUtils;
import com.gkzf.ai.module.crm.controller.admin.simpleCredit.vo.CreditReportSimpleRespVO;
import com.gkzf.ai.module.crm.controller.admin.simpleCredit.vo.CreditReportSimpleSaveReqVO;
import com.gkzf.ai.module.crm.controller.app.simpleCredit.vo.simple.SimplePageReqVO;
import com.gkzf.ai.module.crm.dal.dataobject.creditReport.CreditReportDO;
import com.gkzf.ai.module.crm.dal.dataobject.creditreportsimple.CreditReportSimpleDO;
import com.gkzf.ai.module.crm.service.creditreportsimple.app.SimpleMatchService;
import com.gkzf.ai.module.crm.service.creditreportsimple.app.AppCreditReportSimpleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.gkzf.ai.framework.common.pojo.CommonResult.success;

@Slf4j
@Tag(name = "app后台 - CRM 征信报告简版")
@RestController
@RequestMapping("/app/credit-report-simple")
@Validated
public class CreditReportSimpleAppController {

    @Resource
    private AppCreditReportSimpleService creditReportSimpleService;

    @Resource
    private SimpleMatchService simpleMatchService;

    @PostMapping("/create")
    @Operation(summary = "创建 征信报告简版")
    public CommonResult<Boolean> createCreditReportSimple(@Valid @RequestBody CreditReportSimpleSaveReqVO createReqVO) {
        CreditReportSimpleDO creditReportSimple = creditReportSimpleService.createCreditReportSimple(createReqVO);
        creditReportSimpleService.createCreditReportSimple(createReqVO,creditReportSimple, new CreditReportDO());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得 征信报告简版")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CreditReportSimpleRespVO> getCreditReportSimple(@RequestParam("id") Long id) {
        CreditReportSimpleRespVO creditReportSimple = creditReportSimpleService.getCreditReportSimple(id);
        return success(BeanUtils.toBean(creditReportSimple, CreditReportSimpleRespVO.class));
    }

    @GetMapping("/matchProduct")
    @Operation(summary = "匹配产品")
    @Parameter(name = "id", description = "小程序用户标识", required = true)
    public CommonResult<Object> matchProduct(@RequestParam("userOpenId") String userOpenId) {
        return simpleMatchService.matchProduct(userOpenId);
    }


    @GetMapping("/getCreditAnalysis")
    @Operation(summary = "简版征信分析结果")
    @Parameter(name = "userOpenId", description = "小程序用户标识", required = true)
    public CommonResult<Object> getCreditAnalysis(@RequestParam("simpleId") Long simpleId) {
        CreditReportSimpleRespVO creditReportSimple = creditReportSimpleService.getCreditReportSimple(simpleId);
        String creditReport = creditReportSimpleService.analysisCreditReport(BeanUtils.toBean(creditReportSimple, CreditReportSimpleSaveReqVO.class));
        return success(creditReport);
    }


    @GetMapping("/tryAgain")
    @Operation(summary = "解析失败-重试")
    @Parameter(name = "id", description = "简版ID（不是openId）", required = true, example = "1")
    public CommonResult<Boolean> tryAgain(@RequestParam("id") Long id) {
        creditReportSimpleService.updateStatus(id);
        creditReportSimpleService.tryAgain(id);
        return success(true);
    }


    @GetMapping("/simplePage")
    @Operation(summary = "我的-简版分页")
    public CommonResult<PageResult<CreditReportSimpleRespVO>> simplePage(@Valid SimplePageReqVO simplePageReqVO) {
        PageResult<CreditReportSimpleRespVO> creditReportSimplePage = creditReportSimpleService.getCreditReportSimplePage(simplePageReqVO);
        return success(creditReportSimplePage);
    }
}